package com.yuanmei.charts;

import android.graphics.Color;

import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yuanmei.model.TickQuote;
import com.yuanmei.common.Utils;

import java.util.ArrayList;
import java.util.List;

public class RealTimeTickChartDataSet {
//    private static String TAG = RealTimeTickChartDataSet.class.getName();
    private List<Integer> mXAxisTick;
    private List<Entry> mPriceLineList;
    private List<Entry> mAveragePriceLineList;
    private List<Entry> mYesterdayCloseLineList;
    private CombinedData mTimeSharePriceData;
    private int mAppendXTickCount;

    public RealTimeTickChartDataSet( ) {
        mXAxisTick = new ArrayList<>();
        mPriceLineList = new ArrayList<>();
        mAveragePriceLineList = new ArrayList<>();
        mYesterdayCloseLineList = new ArrayList<>();
        mTimeSharePriceData = new CombinedData();
        mAppendXTickCount = 0;
    }

    public void build(List<TickQuote> tickQuotes, float yClose) {
        mXAxisTick.clear();
        mPriceLineList.clear();
        mAveragePriceLineList.clear();
        mYesterdayCloseLineList.clear();

        float total_volume = 0;
        float total_amount = 0;
        int quotesCount = tickQuotes.size();
        for (int i = 0; i < quotesCount; i++) {
            TickQuote quote = tickQuotes.get(i);
            mXAxisTick.add(quote.getTick());

            float volume = quote.getVolume();
            float amount = quote.getAmount();
            total_volume += volume;
            total_amount += amount;
            mAveragePriceLineList.add(new Entry(i, total_amount / total_volume));

            mPriceLineList.add(new Entry(i, quote.getPrice()));
            mYesterdayCloseLineList.add(new Entry(i, yClose));
        }

        TickQuote lastQuote = tickQuotes.get(quotesCount - 1);
        TickQuote firstQuote = tickQuotes.get(0);
        if (lastQuote.getTick() <= 150000) {
            float secondDiff = Utils.minusOfTick(firstQuote.getTick(), lastQuote.getTick());
            if (lastQuote.getTick() >= 130000) {
                // minus 11:30:00 - 13:00:00
                secondDiff = secondDiff - 1.5f * Utils.SECONDS_PER_HOUR;
            }
            float countPerSecond = (float) quotesCount / secondDiff;
            mAppendXTickCount = (int)((Utils.TRADE_HOUR_PER_DAY - secondDiff/60/60) * 60 * 60 * countPerSecond);
        }

        ArrayList<ILineDataSet> priceLineSets = new ArrayList<>();
        LineDataSet priceLine = new LineDataSet(mPriceLineList, "price");
        TaLib.setCommonMaStyle(priceLine);
        priceLine.setHighlightEnabled(true);
        priceLine.setDrawHorizontalHighlightIndicator(true);
        priceLine.setDrawVerticalHighlightIndicator(true);
        int red = Color.rgb(211, 74, 88);
        priceLine.setHighLightColor(red);
        int snow4 = Color.rgb(139, 137, 137);
        priceLine.setColor(snow4);
        priceLineSets.add(priceLine);

        LineDataSet averagePriceSet = new LineDataSet(mAveragePriceLineList, "average price");
        TaLib.setCommonMaStyle(averagePriceSet);
        int yellow = Color.rgb(238, 173, 14);
        averagePriceSet.setColor(yellow);
        priceLineSets.add(averagePriceSet);

        LineDataSet yClosePriceSet =
                new LineDataSet(mYesterdayCloseLineList, "yesterday close price");
        TaLib.setCommonMaStyle(yClosePriceSet);
        int blue = Color.argb(0xFF, 0x00, 0xB2, 0xEE);
        yClosePriceSet.setColor(blue);
        yClosePriceSet.setLineWidth(1.15f);
        priceLineSets.add(yClosePriceSet);

        mTimeSharePriceData.setData(new LineData(priceLineSets));
    }

    public CombinedData getTaChartData() {
        return mTimeSharePriceData;
    }

    public int getXAxisSize() {
        return mXAxisTick.size() + mAppendXTickCount;
    }
}
